home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: news.athene.co.uk!not-for-mail
- From: "C.J. Scaife" <JOLTSWIFT@Athene.co.uk>
- Subject: Re: Object constuction in function call.
- Message-ID: <31503E74.7C7B@Athene.co.uk>
- Date: Wed, 20 Mar 1996 17:20:52 +0000
- References: <4icmqh$8g6@insosf1.netins.net> <314C4D23.1A40@image.dk>
- Organization: JoltSwift Ltd.
- X-Mailer: Mozilla 2.0GoldB1 (Win95; I)
- MIME-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
-
- Poul Thomas Lomholt wrote:
- >
- > Harold Howe wrote:
- > >
- > > Greetings.
- > >
- > > I need help. If an object is contructed in a function call, how long does it
- > > exist. For example, (this is not what I am doing, but it illustrates the
- > > point).
- > >
- > > class TCorners
- > > {
- > > public:
- > > int w,x,y,z;
- > > TCorners( int a, int b, int c, int d)
- > > { w=a; x=b; y=c; z=d;}
- > > }
- > >
- > > void display_coordinates(TCorners corners)
- > > {
- > > cout << "corners are" << corners.w << corners.x
- > > << corners.y << corners.z ;
- > > }
- > >
- > > int main(void)
- > > {
- > > display_coordinates(TCorners(1,1,20,10));
- > > display_coordinates(TCorners(1,5,25,15));
- > > display_coordinates(TCorners(2,5,20,10));
- > > }
- > >
- > > How many objects exist at the programs end? Are the objects created for the
- > > function call, then nuked afterwards? I think they hang around, in fact I
- > > know they hang around because if I call the display guy about 100 times the
- > > program finally blows up. I would like to use this syntax if possible in the
- > > real program that I am writing.
- > >
- > > Any suggestions would be appreciated.
- > > Harold Howe
- > > hhowe@trgnet.com
- >
- > The objects are created temporarily be sure about that, and
- > hence does not exist, when the program terminates. However it
- > would be a good idea to declare the display_coordinates to take
- > a "const TCorners&" instead of an object.
- >
- > Regards from
- > Poul Thomas Lomholt
-
- The temporary annonymous variables passed as parameters exist only for
- the duration of the statement in which they are constructed. That is
- exactly what you want here because it means they exist until the
- function (to which they are passed) returns.
-
- Beware of returning references to such a temporary object (for an
- example see http://www.Athene.co.uk/Joltswift/csact003.htm).
-
- C. Scaife
-
-